package cn.zn.controller.system;

import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.framework.bean.LoginUser;
import cn.zn.framework.service.JwtTokenService;
import cn.zn.framework.utils.SecurityUtil;
import cn.zn.user.entity.SysUser;
import cn.zn.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 个人信息 业务处理
 *
 * @author zn-admin
 */
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private JwtTokenService tokenService;

    /**
     * 个人信息
     */
    @GetMapping
    public ResultData profile() {
        SysUser theUser = SecurityUtil.getTheUser();
        return ResultUtil.success(theUser);
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePwd")
    public ResultData updatePwd(String oldPassword, String newPassword) {
        LoginUser loginUser = SecurityUtil.getLoginUser();
        String userName = loginUser.getUsername();

        String newEnPassword = userService.updatePassword(userName, oldPassword, newPassword);
        // 更新缓存用户密码
        loginUser.getTheUser().setPassword(newEnPassword);
        tokenService.setLoginUser(loginUser);
        return ResultUtil.success();
    }

}
